const { menuModel, jobAdnMenuModel } = require("../model");

//添加菜单
exports.addMenu = async (req,res)=>{
    const { menuName , menuValue , menuContent } = req.body;
    if(!menuName || !menuValue || !menuContent) {
        return res.status(400).json({
            code:400,
            data:null,
            message:'参数不完整'
        })
    }
    let result1 = await menuModel.findOne({menuName});
    let result2 = await menuModel.findOne({menuValue})
    if(result1 || result2) {
        return res.status(400).json({
            code:400,
            data:null,
            message:'菜单名或者菜单值已存在'
        })
    }
    try {
        let dbBack = new menuModel({
            menuName,
            menuValue,
            menuContent
        })
        await dbBack.save();
        return res.status(200).json({
            code:200,
            data:null,
            message:'添加成功'
        })
    } catch (error) {
        return res.status(500).json({
            code:500,
            data:null,
            message:'服务器错误'
        })
    }
}
//获取菜单按钮
exports.getMenuList = async (req,res)=>{
    const { pageNo=1 , pageSize=5 } = req.query
    try {
        let result = await menuModel.find().skip((pageNo-1)*pageSize).limit(Number(pageSize))
        let total = await menuModel.find().count()
        return res.status(200).json({
            code:200,
            data:{
                list:result,
                total
            },
            message:'获取成功'
        })
    } catch (error) {
        return res.status(500).json({
            code:500,
            data:null,
            message:'服务器错误'
        })
    }
}
//获取菜单详情
exports.getMenuDetail = async (req,res)=>{
    const id = req.query.id
    if(!id) {
        return res.status(400).json({
            code:400,
            data:null,
            message:'id不能为空'
        })
    }
    try {
        let result = await menuModel.findById(id)
        return res.status(200).json({
            code:200,
            data:result,
            message:'获取成功'
        })
    } catch (error) {
        return res.status(500).json({
          code:500,
          data:null,
          message:'服务器错误'  
        })
    }
}
//修改菜单
exports.updateMenu = async (req,res)=>{
    const id = req.query.id
    const { menuName , menuValue , menuContent } = req.body;
    let updateAt = Date.now()
    if(!id) {
        return res.status(400).json({
            code:400,
            data:null,
            message:'id不能为空'
        })
    }
    try {
        await menuModel.findByIdAndUpdate(id,{
            menuName,
            menuValue,
            menuContent,
            UpdateAt:updateAt
        })
        return res.status(200).json({
            code:200,
            data:null,
            message:'修改成功'
        })
    } catch (error) {
        return res.status(500).json({
            code:500,
            data:null,
            message:'修改失败'
        })
    }
}
//删除菜单
exports.deleteMenu = async (req,res)=>{
    const id = req.query.id
    if(!id) {
        return res.status(400).json({
            code:400,
            data:null,
            message:'id不能为空'
        })
    }
   try {
    let result = await jobAdnMenuModel.find({MenuId:id})
    if(result.length>0) {
        return res.status(400).json({
            code:400,
            data:null,
            message:'有职位使用了该菜单，无法删除'
        })
    }
     await menuModel.findByIdAndDelete(id)
    return res.status(200).json({
        code:200,
        data:null,
        message:'删除成功'
    })
   } catch (error) {
    return res.status(500).json({
        code:500,
        data:null,
        message:'服务器错误'
    })
   }
}